//
//  Date+Extension.swift
//  date
//
//  Created by zhangguo on 15/10/25.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import Foundation
extension NSDate {
    
    class func sinaDate(string: String)-> NSDate? {
        
        //转换日期
        let df = NSDateFormatter()
        
        df.locale = NSLocale(localeIdentifier: "en")
        
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        
        //转换成日期
        return df.dateFromString(string)
    }
    
    var dateDesctiption: String {
        
    let cal = NSCalendar.currentCalendar()
        
        //判断是否是今天
        if cal.isDateInToday(self) {
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            
            if delta < 60 {
                return "刚刚"
            }
            if delta < 3600 {
                return "\(delta / 60)分钟前"
            }
            return "\(delta / 3600)小时前"
        }
        
        //日期格式字符串
        var fmtString = " HH:mm"
        //昨天
        if cal.isDateInYesterday(self) {
           fmtString = "昨天" + fmtString
        }else {
            fmtString = "MM-dd" + fmtString
            
            //判断年度
            //        print(cal.components(NSCalendarUnit.Year, fromDate: self))
            let coms = cal.components(NSCalendarUnit.Year, fromDate: self, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
                  
            if coms.year > 0 {
                fmtString = "yyyy-" + fmtString
            }
       
        }
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        
        df.dateFormat = fmtString
        
        return df.stringFromDate(self)
    }

}